{ "metadata": { "name": "", "signature": "sha256:0d0a916691e6035e241a2d6097ddd91a4515843d8bdb4727bed7f2fa742e7912" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": true, "input": [ "import time" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "%%time\n", "\n", "time.sleep(.138)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "CPU times: user 446 \u00b5s, sys: 1 \u00b5s, total: 447 \u00b5s\n", "Wall time: 138 ms\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "%%time\n", "\n", "time.sleep(.000639 * 10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "CPU times: user 311 \u00b5s, sys: 0 ns, total: 311 \u00b5s\n", "Wall time: 6.52 ms\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": true, "input": [ "class Symbol(str):\n", " \"Class to define symbols, which should be unique items\"\n", " def __repr__(self):\n", " return str.__repr__(self)[1:-1] # don't show quotation marks\n", " \n", "EmptyList = Symbol(\"()\")\n", "void = Symbol(\"\")\n", " \n", "class Cons(object):\n", " \"A cell/link used to construct linked-list\"\n", " def __init__(self, car, cdr):\n", " self.car = car\n", " self.cdr = cdr\n", " def __repr__(self):\n", " if self.car == \"closure-exp\":\n", " return \"#\"\n", " retval = \"\"\n", " current = self\n", " while current is not EmptyList and isinstance(current, Cons):\n", " if retval != \"\":\n", " retval += \" \"\n", " retval += str(current.car) # recursion here!\n", " current = current.cdr\n", " if current is not EmptyList:\n", " retval += \" . \" + str(current)\n", " return \"(%s)\" % retval\n", " \n", "class String(str):\n", " \"Class to wrap strings so that we can define repr\"\n", " def __repr__(self):\n", " return '\"%s\"' % str.__repr__(self)[1:-1]\n", " def __str__(self):\n", " return self.__repr__()\n", "\n", "def List(*args):\n", " \"Create a linked-list of items\"\n", " retval = EmptyList\n", " for arg in reversed(args):\n", " retval = cons(arg, retval)\n", " return retval\n", " \n", "def car(exp):\n", " return exp.car\n", "def cadr(exp):\n", " return exp.cdr.car\n", "def caddr(exp):\n", " return exp.cdr.cdr.car\n", "def cadddr(exp):\n", " return exp.cdr.cdr.cdr.car\n", "\n", "def cdr(exp):\n", " return exp.cdr\n", "def cddr(exp):\n", " return exp.cdr.cdr\n", "def cdddr(exp):\n", " return exp.cdr.cdr.cdr\n", "def cddddr(exp):\n", " return exp.cdr.cdr.cdr.cdr\n", "\n", "def cons(item1, item2):\n", " return Cons(item1, item2)\n", "\n", "def length(item, handler):\n", " current = item\n", " count = 0\n", " while current is not EmptyList and isinstance(current, Cons):\n", " current = current.cdr\n", " count += 1\n", " if current is not EmptyList:\n", " return handler(\"Attempt to take length of improper list\")\n", " return count\n", "\n", "def rac(item):\n", " current = item\n", " previous = None\n", " while current is not EmptyList:\n", " previous = current.car\n", " current = current.cdr\n", " return previous\n", "\n", "## We use \"_q\" in Python to represent \"?\" in Scheme.\n", "\n", "def pair_q(item):\n", " return isinstance(item, Cons)\n", "\n", "def null_q(lst):\n", " return lst is EmptyList" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 28 }, { "cell_type": "code", "collapsed": true, "input": [ "def list_to_vector(lst):\n", " retval = []\n", " current = lst\n", " while current is not EmptyList:\n", " retval.append(current.car)\n", " current = current.cdr\n", " return retval\n", "\n", "def extend_env(vars, vals, env):\n", " return [dict(map(list,zip(list_to_vector(vars), \n", " list_to_vector(vals)))), env]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "extend_env(List(\"x\", \"y\"), List(1, 2), [])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "[{'x': 1, 'y': 2}, []]" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": true, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }